package cn.sinata.wanglin.village.activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.TimeUtils;

import java.util.ArrayList;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.CommentBean;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.RecyclerListWithFramLayoutActivity;

public class NewinfoCommentListActivity extends RecyclerListWithFramLayoutActivity {
    private LinearLayout ly_zannum_action;//点赞操作
    private TextView tv_gotocomment;//拉起评论操作
    private int pageNo = 0;
    private HFRecyclerAdapter showCommentAdapter;
    private ArrayList<CommentBean> showCommentdatas = new ArrayList<CommentBean>();
    private String id;
    private String type;
    private boolean isEdit = false;

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if (showCommentAdapter == null) {
            showCommentAdapter = new HFRecyclerAdapter<CommentBean>(showCommentdatas, R.layout.item_commentlist) {
                @Override
                public void onBind(final int position, final CommentBean data, ViewHolder holder) {
                    if (!TextUtils.isEmpty(data.getIsanonymity())) {
                        if ("1".equals(data.getIsanonymity())) {
                            ((SimpleDraweeView) holder.bind(R.id.iv_headview)).setImageURI(Uri.parse("res://mipmap/" + R.mipmap.ic_isanonymity));
                        } else {
                            ((SimpleDraweeView) holder.bind(R.id.iv_headview)).setImageURI(data.getHeadUrl());
                        }
                    } else {
                        ((SimpleDraweeView) holder.bind(R.id.iv_headview)).setImageURI(data.getHeadUrl());
                    }
                    holder.bind(R.id.iv_headview).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if("1".equals(data.getIsanonymity())){
                                return;
                            }
                            ActivityUtil.create(mContext).go(AllUserInfoActivity.class).put("friendID",data.getUserID())
                                    .start();

                        }
                    });

                    if (!TextUtils.isEmpty(data.getUserName())) {
                        holder.setText(R.id.tv_username, data.getUserName());
                    } else {
                        holder.setText(R.id.tv_username, Constant.DEFULT_NICKNAME);
                    }
                    if (!TextUtils.isEmpty("" + data.getCreateDate())) {
                        holder.setText(R.id.tv_time, TimeUtils.getMilliTimeYMD(data.getCreateDate()));
                    } else {
                        holder.setText(R.id.tv_time, "暂无时间");
                    }

                    if (!TextUtils.isEmpty(data.getContent())) {
                        //回复类型（0评论文章,1回复评论）
                        if("1".equals(data.getType())){
                            String commentPerson=data.getParentName()+":";
                            String allcomment="回复"+commentPerson+data.getContent();
                            SpannableStringBuilder builder = new SpannableStringBuilder(allcomment);
                            ForegroundColorSpan blueSpan = new ForegroundColorSpan(getResources().getColor(R.color.commentListBlueColor));
                            ForegroundColorSpan blackSpan = new ForegroundColorSpan(getResources().getColor(R.color.commentColor));
                            builder.setSpan(blackSpan,0,2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                            builder.setSpan(blueSpan,2,2+commentPerson.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                            builder.setSpan(blackSpan,2+commentPerson.length(),allcomment.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                            Spannable span = EaseSmileUtils.getSmiledText(mContext,builder);
                            ((TextView)holder.bind(R.id.tv_content)).setText(span, TextView.BufferType.SPANNABLE);
                        }else{
                            Spannable span = EaseSmileUtils.getSmiledText(mContext,data.getContent());
                            ((TextView)holder.bind(R.id.tv_content)).setText(span, TextView.BufferType.SPANNABLE);
                        }
                    } else {
                        holder.setText(R.id.tv_content, "暂无内容");
                    }

                }
            };
        }
        showCommentAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext)
                        .go(EditCommentOtherActivity.class)
                        .put("id", id)
                        .put("toUser", showCommentdatas.get(position).getUserID())
                        .put("externalType",type)
                        .put("commentId",showCommentdatas.get(position).getId())
                        .put("type",1)
                        .put("commentUsername",showCommentdatas.get(position).getUserName())
                        .startForResult(12);
            }
        });
        return showCommentAdapter;
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData();
    }

    private void getData() {
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getComment(userid,id,type,pageNo).subscribe(new ResultDataSubscriber<ArrayList<CommentBean>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<CommentBean> commentBeen) {
                if (pageNo == 0) {
                    showCommentdatas.clear();
                }
                if (commentBeen.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                showCommentdatas.addAll(commentBeen);
                showCommentAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("评论列表");
        id = getIntent().getStringExtra("id");
        type= getIntent().getStringExtra("type");
        View flView = LayoutInflater.from(this).inflate(R.layout.bottom_comment_and_zan, null);
        tv_gotocomment = (TextView) flView.findViewById(R.id.tv_gotocomment);
        ly_zannum_action = (LinearLayout) flView.findViewById(R.id.ly_zannum_action);
        ly_zannum_action.setVisibility(View.GONE);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM;
        getFl_prent_list().addView(flView, params);

        tv_gotocomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(mContext)
                        .go(EditCommentOtherActivity.class)
                        .put("id", id)
                        .put("toUser", "-1")
                        .put("externalType", type)
                        .startForResult(12);
            }
        });
        showDialog();
        getData();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 12:
                    isEdit = true;
                    pullDownRefresh();
                    break;
            }
        }
    }

    @Override
    public void finish() {
        if (isEdit) {
            setResult(RESULT_OK);
        }
        super.finish();
    }
}
